﻿'
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2008
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.Collections.Generic
Imports System.Web
Imports System.Xml
Imports System.Security
Imports System.IO
Imports System.Globalization

Namespace DotNetNuke.Modules.IWebCF

    <IWebContract(CoreBindings.ws2007HttpBinding, Address:="", Name:="CoreService")> _
<ServiceContract(Namespace:="http://webservices.DotNetNuke.com/2007/12/CoreService")> _
Public Interface IWebCoreService

        <OperationContract(Action:="PortalsOnServer")> _
        Function PortalsOnServer(ByVal msg As IWebRequest) As IWebResponse(Of String)
        <OperationContract(Action:="GetVersion")> _
        Function GetVersion(ByVal msg As IWebRequest) As IWebResponse(Of String)
        '<OperationContract(Action:="EncryptData")> _
        'Function EncryptData(ByVal msg As EncryptionRequest) As IWebResponse(Of String)
        '<OperationContract(Action:="UnEncryptData")> _
        'Function UnEncryptData(ByVal msg As EncryptionRequest) As IWebResponse(Of String)

    End Interface



    <AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
    Partial Public Class CoreService
        Implements IWebCoreService

        <IWebMethod( _
            Company:="DotNetNuke", _
            Application:="IWeb", _
            Name:="GetVersion", _
            Description:="Retrieves IWeb Version", _
            Group1:="IWEB Misc", Group2:="Portal", _
            DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function GetVersion(ByVal msg As IWebRequest) As IWebResponse(Of String) Implements IWebCoreService.GetVersion
            Dim strVersion As String = ""
            Try

                Dim objIWebAuthendication As New IWebAuthentication(msg.Header)
                If Not objIWebAuthendication.ValidAndAuthorized() Then
                    Return New IWebResponse(Of String)("Not Authorized")
                End If

                Dim objDesktopModules As New Entities.Modules.DesktopModuleController
                Dim objDesktopModuleInfo As New Entities.Modules.DesktopModuleInfo
                Dim arr As ArrayList = objDesktopModules.GetDesktopModules()
                For Each objDesktopModuleInfo In arr
                    If objDesktopModuleInfo.ModuleName = "IWebCF" Then
                        strVersion = objDesktopModuleInfo.Version
                    End If
                Next
            Catch ex As Exception
                strVersion = ex.Message
            End Try

            Return New IWebResponse(Of String)(strVersion)
        End Function

        <IWebMethod( _
            Company:="DotNetNuke", _
            Application:="IWeb", _
            Name:="PortalsOnServer", _
            Description:="Retrieves Portals on Server", _
            Group1:="General", Group2:="Portal", _
            DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        Public Function PortalsOnServer(ByVal msg As IWebRequest) As IWebResponse(Of String) Implements IWebCoreService.PortalsOnServer
            Dim strResponse As String = ""
            Try
                Dim objIWebAuthendication As New IWebAuthentication(msg.Header)
                If Not objIWebAuthendication.ValidAndAuthorized() Then
                    Return New IWebResponse(Of String)("0,Not Authorized")
                End If

                Dim arrPortals As ArrayList
                Dim colPortalInfo As New PortalInfo()
                Dim colPortalController As New PortalController()
                arrPortals = colPortalController.GetPortals()

                Dim i As Integer
                For i = 0 To (arrPortals.Count - 1)
                    colPortalInfo = CType(arrPortals(i), PortalInfo)
                    strResponse = strResponse & colPortalInfo.PortalID & "," & colPortalInfo.PortalName & ","
                Next i

                strResponse = strResponse.Substring(0, strResponse.Length - 1)
            Catch ex As Exception
                strResponse = ex.Message
            End Try

            Return New IWebResponse(Of String)(strResponse)
        End Function


        '<IWebMethod( _
        '   Company:="DotNetNuke", _
        '   Application:="IWeb", _
        '   Name:="EncryptData", _
        '   Description:="Encrypts Data", _
        '   Group1:="IWEB Misc", Group2:="Portal", _
        '   DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        'Public Function EncryptData(ByVal msg As EncryptionRequest) As IWebResponse(Of String) Implements IWebCoreService.EncryptData
        '    Dim strResponse As String = ""
        '    Try
        '        Dim objIWebAuthendication As New IWebAuthentication(msg.Header)
        '        If Not objIWebAuthendication.ValidAndAuthorized() Then
        '            Return New IWebResponse(Of String)("Not Authorized")
        '        End If

        '        Dim objIWebEncryption As New IWebEncryption()
        '        strResponse = objIWebEncryption.Encryption(msg.Text, msg.Key)
        '        strResponse = HttpUtility.UrlEncode(strResponse)
        '    Catch ex As Exception
        '        strResponse = ex.Message
        '    End Try
        '    Return New IWebResponse(Of String)(strResponse)
        'End Function

        '<IWebMethod( _
        '   Company:="DotNetNuke", _
        '   Application:="IWeb", _
        '   Name:="UnEncryptData", _
        '   Description:="Decrypts Data", _
        '   Group1:="IWEB Misc", Group2:="Portal", _
        '   DefaultSecuritySetting:=IWebMethodAttribute.MethodSecurityLevels.Host)> _
        'Public Function UnEncryptData(ByVal msg As EncryptionRequest) As IWebResponse(Of String) Implements IWebCoreService.UnEncryptData
        '    Dim strResponse As String = ""
        '    Try
        '        Dim objIWebAuthendication As New IWebAuthentication(msg.Header)
        '        If Not objIWebAuthendication.ValidAndAuthorized() Then
        '            Return New IWebResponse(Of String)("Not Authorized")
        '        End If

        '        Dim objIWebEncryption As New IWebEncryption()
        '        strResponse = objIWebEncryption.DeCrypt(HttpUtility.UrlDecode(msg.Text), msg.Key).ToString
        '        strResponse = HttpUtility.UrlEncode(strResponse)
        '    Catch ex As Exception
        '        strResponse = ex.Message
        '    End Try
        '    Return New IWebResponse(Of String)(strResponse)
        'End Function

    End Class
End Namespace
